package MainPage;

import java.awt.Cursor;
import java.sql.SQLException;
import java.util.Arrays;

import javax.swing.JOptionPane;

public class registration extends javax.swing.JFrame {

	String SQL,firstname,lastname,username,gender,dob,address,city,country,password;
	int age;
	char[]getPassword;
	
    /** Creates new form REGISTRATION */
    public registration() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        headerLabel = new javax.swing.JLabel();
        fnameLabel = new javax.swing.JLabel();
        fnameTextField = new javax.swing.JTextField();
        lnameLabel = new javax.swing.JLabel();
        lnameTextField = new javax.swing.JTextField();
        usernameLabel = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        genderLabel = new javax.swing.JLabel();
        genderComboBox = new javax.swing.JComboBox();
        DOBLabel = new javax.swing.JLabel();
        dayComboBox = new javax.swing.JComboBox();
        monthComboBox = new javax.swing.JComboBox();
        yearComboBox = new javax.swing.JComboBox();
        ageLabel = new javax.swing.JLabel();
        ageTextField = new javax.swing.JTextField();
        addressLabel = new javax.swing.JLabel();
        addressScrollPane = new javax.swing.JScrollPane();
        addressText = new javax.swing.JTextArea();
        cityLabel = new javax.swing.JLabel();
        cityTextField = new javax.swing.JTextField();
        countryLabel = new javax.swing.JLabel();
        countryComboBox = new javax.swing.JComboBox();
        pwLabel = new javax.swing.JLabel();
        pwPasswordField = new javax.swing.JPasswordField();
        confirmpwLabel = new javax.swing.JLabel();
        confirmpwPasswordField = new javax.swing.JPasswordField();
        borderLabel = new javax.swing.JLabel();
        registerButton = new javax.swing.JButton();
        jbtnhome = new javax.swing.JButton();
        backgroundLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("My Calendar");
        setAlwaysOnTop(true);

        headerLabel.setFont(new java.awt.Font("Algerian", 1, 36));
        headerLabel.setForeground(new java.awt.Color(102, 0, 0));
        headerLabel.setText("REGISTRATION");
        headerLabel.setBounds(480, 50, 330, 70);
        jLayeredPane1.add(headerLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        fnameLabel.setText("First Name");
        fnameLabel.setBounds(410, 190, 80, 15);
        jLayeredPane1.add(fnameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        fnameTextField.setBounds(590, 190, 280, 25);
        jLayeredPane1.add(fnameTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        lnameLabel.setText("Last Name");
        lnameLabel.setBounds(410, 240, 90, 15);
        jLayeredPane1.add(lnameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        lnameTextField.setBounds(590, 240, 280, 25);
        jLayeredPane1.add(lnameTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        usernameLabel.setText("Username");
        usernameLabel.setBounds(410, 290, 80, 15);
        jLayeredPane1.add(usernameLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        usernameTextField.setBounds(590, 290, 280, 25);
        jLayeredPane1.add(usernameTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genderLabel.setText("Gender");
        genderLabel.setBounds(410, 330, 70, 15);
        jLayeredPane1.add(genderLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genderComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "male", "female" }));
        genderComboBox.setBounds(590, 330, 80, 25);
        jLayeredPane1.add(genderComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        DOBLabel.setText("D.O.B");
        DOBLabel.setBounds(410, 370, 50, 15);
        jLayeredPane1.add(DOBLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        dayComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<DAY>", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        dayComboBox.setBounds(590, 370, 80, 25);
        jLayeredPane1.add(dayComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        monthComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<MONTH>", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        monthComboBox.setBounds(680, 370, 100, 25);
        jLayeredPane1.add(monthComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        yearComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<YEAR>", "1990", "1989", "1988", "1987", "1986", "1985", "1984", "1983", "1982", "1981", "1980", "1979", "1978", "1977", "1976", "1975", "1974", "1973", "1972", "1971", "1970" }));
        yearComboBox.setBounds(790, 370, 80, 25);
        jLayeredPane1.add(yearComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        ageLabel.setText("Age");
        ageLabel.setBounds(740, 330, 45, 20);
        jLayeredPane1.add(ageLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        ageTextField.setBounds(790, 330, 80, 25);
        jLayeredPane1.add(ageTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        addressLabel.setText("Address");
        addressLabel.setBounds(410, 420, 60, 15);
        jLayeredPane1.add(addressLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        addressText.setColumns(20);
        addressText.setRows(5);
        addressScrollPane.setViewportView(addressText);

        addressScrollPane.setBounds(590, 410, 280, 50);
        jLayeredPane1.add(addressScrollPane, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cityLabel.setText("City");
        cityLabel.setBounds(410, 480, 40, 15);
        jLayeredPane1.add(cityLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        cityTextField.setBounds(590, 480, 280, 25);
        jLayeredPane1.add(cityTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        countryLabel.setText("Country");
        countryLabel.setBounds(410, 530, 60, 15);
        jLayeredPane1.add(countryLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        countryComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Malaysian", "Non-Malaysian" }));
        countryComboBox.setBounds(590, 520, 280, 25);
        jLayeredPane1.add(countryComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        pwLabel.setText("Password");
        pwLabel.setBounds(410, 570, 70, 15);
        jLayeredPane1.add(pwLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        pwPasswordField.setBounds(590, 560, 280, 25);
        jLayeredPane1.add(pwPasswordField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        confirmpwLabel.setText("Confirm Password");
        confirmpwLabel.setBounds(410, 610, 130, 15);
        jLayeredPane1.add(confirmpwLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        confirmpwPasswordField.setBounds(590, 610, 280, 25);
        jLayeredPane1.add(confirmpwPasswordField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        borderLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        borderLabel.setBounds(330, 170, 620, 500);
        jLayeredPane1.add(borderLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        registerButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/registration_icon.jpg")));
        registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        registerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	firstPage.playAudio(getClass().getResource("/resource/buttonHit.wav").getFile());
                registerButtonActionPerformed(evt);
            }
        });
        registerButton.setBounds(990, 630, 150, 40);
        jLayeredPane1.add(registerButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbtnhome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/home-button.jpg"))); // NOI18N
        jbtnhome.setBounds(20, 20, 50, 50);
        jbtnhome.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        jbtnhome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	firstPage.playAudio(getClass().getResource("/resource/buttonHit.wav").getFile());
                homeButtonActionPerformed(evt);
            }
        });
        jLayeredPane1.add(jbtnhome, javax.swing.JLayeredPane.DEFAULT_LAYER);

        backgroundLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/registration2.jpg"))); // NOI18N
        backgroundLabel.setBounds(0, 0, 1270, 800);
        jLayeredPane1.add(backgroundLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1263, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    void performMainPage()
	{
		userMainPage main_page=new userMainPage()
		{
			public String getParameter(String name)
			{
		           if (name.equalsIgnoreCase("msg"))
		           {
		              return "Local Frame";
		           }
		           return null;
		    }
		};
		main_page.setSize(userMainPage.screensize.width,userMainPage.screensize.height);
		main_page.setTitle("My Calender");
		this.dispose();
		main_page.setVisible(true);
	}
    
    private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {      
    	if(!Arrays.equals(pwPasswordField.getPassword(),confirmpwPasswordField.getPassword()))
    	{
    		JOptionPane.showMessageDialog(this,"Password does not match. Please Try again.","Error Message",
	                JOptionPane.ERROR_MESSAGE);
    		pwPasswordField.setText("");
    		confirmpwPasswordField.setText("");
    	}
    	else
    	{
    	firstname=fnameTextField.getText();
    	lastname=lnameTextField.getText();
    	username=usernameTextField.getText();
    	switch(genderComboBox.getSelectedIndex())
    	{
    	case 0:
    	    gender="M";
    	    break;
    	case 1:
    	    gender="F";
    	    break;
    	}
    	String day=dayComboBox.getSelectedItem().toString();
    	String month=monthComboBox.getSelectedItem().toString();
    	String year=yearComboBox.getSelectedItem().toString();
    	dob=day+"-"+month+"-"+year;
        age=Integer.parseInt(ageTextField.getText());
    	address=addressText.getText();
    	city=cityTextField.getText();
    	country=countryComboBox.getSelectedItem().toString();
    	
    	getPassword=pwPasswordField.getPassword();
    	for(int a=0;a<getPassword.length;a++)
    	{
    		if(a==0)
    			password=Character.toString(getPassword[a]);
    		else
    			password=password+Character.toString(getPassword[a]);
    	}
    	SQL="INSERT INTO USER_DETAIL (USER_FNAME,USER_LNAME,USER_GENDER,USER_AGE,USER_DOB,USER_ADDRESS,USER_CITY,USER_COUNTRY,USER_USERNAME,USER_PASSWORD)" +
    		"VALUES ('"+firstname+"','"+lastname+"','"+gender+"','"+age+"','"+dob+"','"+address+"','"+city+"','"+country+"','"+username+"','"+password+"')";
    	try {
			userMainPage.stmt.executeQuery(SQL);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	JOptionPane.showMessageDialog(null, "Congratulations! You have been register! Please login at Main Page!");
        performMainPage();
        }
    }   
    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) 
    {                                               
         performMainPage();
    } 
     
    
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new registration().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel DOBLabel;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JScrollPane addressScrollPane;
    private javax.swing.JLabel ageLabel;
    private javax.swing.JTextField ageTextField;
    private javax.swing.JLabel backgroundLabel;
    private javax.swing.JLabel borderLabel;
    private javax.swing.JLabel cityLabel;
    private javax.swing.JTextField cityTextField;
    private javax.swing.JLabel confirmpwLabel;
    private javax.swing.JPasswordField confirmpwPasswordField;
    private javax.swing.JComboBox countryComboBox;
    private javax.swing.JLabel countryLabel;
    private javax.swing.JComboBox dayComboBox;
    private javax.swing.JLabel fnameLabel;
    private javax.swing.JTextField fnameTextField;
    private javax.swing.JComboBox genderComboBox;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JTextArea addressText;
    private javax.swing.JButton jbtnhome;
    private javax.swing.JLabel lnameLabel;
    private javax.swing.JTextField lnameTextField;
    private javax.swing.JComboBox monthComboBox;
    private javax.swing.JLabel pwLabel;
    private javax.swing.JPasswordField pwPasswordField;
    private javax.swing.JButton registerButton;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameTextField;
    private javax.swing.JComboBox yearComboBox;
    // End of variables declaration

}
